
	
proc fillMenus {} {
	if {![winfo exists .entrymenu]} {
	menu .entrymenu -tearoff 0
	foreach {event shortcut} {Cut Ctrl-X  Copy Ctrl-C  Paste Ctrl-V} {
		.entrymenu add command -label [::msgcat::mc $event] -command "event generate \$::textprops::currentEntry <<$event>>" -accelerator $shortcut
	}
	.entrymenu add command -label [::msgcat::mc Delete] -command "event generate \$::textprops::currentEntry <Key-Delete>" -accelerator Del
	}
	if {![winfo exists .textmenu]} {
		menu .textmenu -tearoff 0
		StdEditMenu .textmenu "\$::textprops::currentText"
		.textmenu add separator
		.textmenu add command -label [::msgcat::mc "Insert file..."] -command [namespace code insertFile ]
	}
}

proc insertFile {} {
	variable currentText
	set w $currentText
	set filename [tk_getOpenFile -title [::msgcat::mc "Insert a file"]\
		-filetypes [list [list [::msgcat::mc "Text files"] {txt}]\
				  [list [::msgcat::mc "All files"] {*}]]]
	if {![string length $filename]} return
	set f [open $filename]
    set oldSeparator [$w cget -autoseparators]
	if { $oldSeparator } {
	     $w configure -autoseparators 0
	     $w edit separator
	}
	$w insert insert [read $f]
	if { $oldSeparator } {
	     $w edit separator
	     $w configure -autoseparators 1
	}
	close $f
}							

proc textRightButton {widget x y} {
	variable currentText
	.textmenu entryconfigure [::msgcat::mc "Insert file..."] -state [$widget cget -state]
	set currentText $widget
	tk_popup .textmenu $x $y
}	
proc entryRightButton {widget x y} {
	variable currentEntry
	if {[$widget cget -state] eq "disabled"||[catch {clipboard get}]} {
		.entrymenu entryconfigure [::msgcat::mc Paste] -state disabled
	} else {
		.entrymenu entryconfigure [::msgcat::mc Paste] -state normal
	}	
	if {[$widget selection present]} {
		if {[string length [$widget cget -show]]==0} {
			.entrymenu entryconfigure [::msgcat::mc Copy] -state normal
		} else {
			.entrymenu entryconfigure [::msgcat::mc Copy] -state disabled
		}	
		if {[$widget cget -state] ne "disabled"} {
			if {[string length [$widget cget -show]]==0} {
				.entrymenu entryconfigure [::msgcat::mc Cut] -state normal
			} else {
				.entrymenu entryconfigure [::msgcat::mc Cut] -state disabled
			}	
			.entrymenu entryconfigure [::msgcat::mc Delete] -state normal
		} else {	
			.entrymenu entryconfigure [::msgcat::mc Cut] -state disabled
			.entrymenu entryconfigure [::msgcat::mc Delete] -state disabled
		}
	} else {	
		.entrymenu entryconfigure [::msgcat::mc Cut] -state disabled
		.entrymenu entryconfigure [::msgcat::mc Delete] -state disabled
		.entrymenu entryconfigure [::msgcat::mc Copy] -state disabled
	}	
	set currentEntry $widget
	tk_popup .entrymenu $x $y	
}

proc StdEditMenu {menu widget} {
	foreach {event shortcut} {Undo Ctrl-Z Redo Ctrl-Shift-Z Cut
		Ctrl-X Copy Ctrl-C Paste Ctrl-V} {
		$menu add command -label [::msgcat::mc $event] -command "event generate $widget <<$event>>" -accelerator $shortcut
	}	
	$menu insert [::msgcat::mc Redo] separator 
	$menu add command -label [::msgcat::mc Delete] -command "event generate $widget <Key-Delete>" -accelerator Del
	$menu configure -postcommand "::textprops::editMenuPost $menu $widget"

}

proc editMenuPost {menu widget} {
	set hasSelection [llength [$widget tag ranges sel]] 
	if {$hasSelection} {
		$menu entryconfigure [::msgcat::mc Copy] -state normal
	} else {
		$menu entryconfigure [::msgcat::mc Copy] -state disabled
	}	
	if {[$widget cget -state] eq "disabled"} {
		foreach entry {Undo Redo Paste Cut Delete} {
			$menu entryconfigure [::msgcat::mc $entry] -state disabled
	    }	
	} else {
		if {[catch {clipboard get}]} {
			$menu entryconfigure [::msgcat::mc Paste] -state disabled
		} else {	
			$menu entryconfigure [::msgcat::mc Paste] -state normal
		}	
		if {$hasSelection} {
			$menu entryconfigure [::msgcat::mc Cut] -state normal
			$menu entryconfigure [::msgcat::mc Delete] -state normal
		} else {	
			$menu entryconfigure [::msgcat::mc Cut] -state disabled
			$menu entryconfigure [::msgcat::mc Delete] -state disabled
		}	
		if {[$widget cget -undo]} {
			if {[$widget edit modified]} {
				$menu entryconfigure [::msgcat::mc Undo] -state normal
			} else {
				$menu entryconfigure [::msgcat::mc Undo] -state disabled
			}	
			$menu entryconfigure [::msgcat::mc Redo] -state normal
		} else {
			$menu entryconfigure [::msgcat::mc Undo] -state disabled
			$menu entryconfigure [::msgcat::mc Redo] -state disabled
		}	
			
	}	
}

#bind Entry <Button-3> [namespace code {entryRightButton %W %X %Y}]
#bind Text <Button-3> [namespace code {textRightButton %W %X %Y}]
#fillMenus
#}
package provide textprops 1.0
